# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=pythran
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
pkgver=0.12.1
pkgrel=1
pkgdesc="Ahead of Time compiler for numeric kernels (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://pythran.readthedocs.io/"
license=('BSD')
depends=("${MINGW_PACKAGE_PREFIX}-boost"
         "${MINGW_PACKAGE_PREFIX}-xsimd"
         "${MINGW_PACKAGE_PREFIX}-python-beniget"
         "${MINGW_PACKAGE_PREFIX}-python-gast"
         "${MINGW_PACKAGE_PREFIX}-python-ply"
         "${MINGW_PACKAGE_PREFIX}-python-numpy")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools")
source=("https://pypi.python.org/packages/source/${_realname:0:1}/${_realname}/${_realname}-${pkgver}.tar.gz"
        001-fix-default-config.patch)
sha256sums=('702c2701187cfb38f66c0c20cc85d04d0e156d260a8d92892da65947faa5360e'
            'aa03979a1ede62e1ea19d17eeaa3208afa4ddfa89bc0f70435dca41be7f8730f')

prepare() {
  cd ${_realname}-${pkgver}
  patch -p1 -i "${srcdir}"/001-fix-default-config.patch

  rm -rf "${srcdir}"/python-build-${MSYSTEM} | true
  cp -r "${srcdir}/${_realname}-${pkgver}" "${srcdir}/python-build-${MSYSTEM}"
}

build() {
  cd "${srcdir}"/python-build-${MSYSTEM}
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python setup.py build
}

package() {
  cd python-build-${MSYSTEM}
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python setup.py install --prefix=${MINGW_PREFIX} --root=${pkgdir} --optimize=1

  rm -r "$pkgdir"${MINGW_PREFIX}/lib/python*/site-packages/pythran/{boost,xsimd} # Remove bundled boost and xsimd

  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*-script.py; do
    # Remove shebang line
    sed -e '1 { s/^#!.*$// }' -i "${_f}"
  done

  install -Dm644 LICENSE \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE"
}
